home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsshade.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  15.8 KB  |  540 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsshade.c,v 1.3 2000/09/19 19:00:32 lpd Exp $ */
  20. /* Constructors for shadings */
  21. #include "gx.h"
  22. #include "gscspace.h"
  23. #include "gserrors.h"
  24. #include "gsstruct.h"
  25. #include "gxdevcli.h"
  26. #include "gxcpath.h"
  27. #include "gxcspace.h"
  28. #include "gxdcolor.h"        /* for filling background rectangle */
  29. #include "gxistate.h"
  30. #include "gxpaint.h"
  31. #include "gxpath.h"
  32. #include "gxshade.h"
  33. #include "gzpath.h"
  34. #include "gzcpath.h"
  35.  
  36. /* ================ Initialize shadings ================ */
  37.  
  38. /* ---------------- Generic services ---------------- */
  39.  
  40. /* GC descriptors */
  41. private_st_shading();
  42. private_st_shading_mesh();
  43.  
  44. /* Check ColorSpace, BBox, and Function (if present). */
  45. /* Free variables: params. */
  46. private int
  47. check_CBFD(const gs_shading_params_t * params,
  48.        const gs_function_t * function, const float *domain, int m)
  49. {
  50.     int ncomp = gs_color_space_num_components(params->ColorSpace);
  51.  
  52.     if (ncomp < 0 ||
  53.     (params->have_BBox &&
  54.      (params->BBox.p.x > params->BBox.q.x ||
  55.       params->BBox.p.y > params->BBox.q.y))
  56.     )
  57.     return_error(gs_error_rangecheck);
  58.     if (function != 0) {
  59.     if (function->params.m != m || function->params.n != ncomp)
  60.         return_error(gs_error_rangecheck);
  61.     /*
  62.      * The Adobe documentation says that the function's domain must
  63.      * be a superset of the domain defined in the shading dictionary.
  64.      * However, Adobe implementations apparently don't necessarily
  65.      * check this ahead of time; therefore, we do the same.
  66.      */
  67. #if 0                /*************** */
  68.     {
  69.         int i;
  70.  
  71.         for (i = 0; i < m; ++i)
  72.         if (function->params.Domain[2 * i] > domain[2 * i] ||
  73.             function->params.Domain[2 * i + 1] < domain[2 * i + 1]
  74.             )
  75.             return_error(gs_error_rangecheck);
  76.     }
  77. #endif /*************** */
  78.     }
  79.     return 0;
  80. }
  81.  
  82. /* Check parameters for a mesh shading. */
  83. private int
  84. check_mesh(const gs_shading_mesh_params_t * params)
  85. {
  86.     if (!data_source_is_array(params->DataSource)) {
  87.     int code = check_CBFD((const gs_shading_params_t *)params,
  88.                   params->Function, params->Decode, 1);
  89.  
  90.     if (code < 0)
  91.         return code;
  92.     switch (params->BitsPerCoordinate) {
  93.         case  1: case  2: case  4: case  8:
  94.         case 12: case 16: case 24: case 32:
  95.         break;
  96.         default:
  97.         return_error(gs_error_rangecheck);
  98.     }
  99.     switch (params->BitsPerComponent) {
  100.         case  1: case  2: case  4: case  8:
  101.         case 12: case 16:
  102.         break;
  103.         default:
  104.         return_error(gs_error_rangecheck);
  105.     }
  106.     }
  107.     return 0;
  108. }
  109.  
  110. /* Check the BitsPerFlag value.  Return the value or an error code. */
  111. private int
  112. check_BPF(const gs_data_source_t *pds, int bpf)
  113. {
  114.     if (data_source_is_array(*pds))
  115.     return 2;
  116.     switch (bpf) {
  117.     case 2: case 4: case 8:
  118.     return bpf;
  119.     default:
  120.     return_error(gs_error_rangecheck);
  121.     }
  122. }
  123.  
  124. /* Initialize common shading parameters. */
  125. private void
  126. shading_params_init(gs_shading_params_t *params)
  127. {
  128.     params->ColorSpace = 0;    /* must be set by client */
  129.     params->Background = 0;
  130.     params->have_BBox = false;
  131.     params->AntiAlias = false;
  132. }
  133.  
  134. /* Initialize common mesh shading parameters. */
  135. private void
  136. mesh_shading_params_init(gs_shading_mesh_params_t *params)
  137. {
  138.     shading_params_init((gs_shading_params_t *)params);
  139.     data_source_init_floats(¶ms->DataSource, NULL, 0);/* client must set */
  140.     /* Client must set BitsPerCoordinate and BitsPerComponent */
  141.     /* if DataSource is not an array. */
  142.     params->Decode = 0;
  143.     params->Function = 0;
  144. }
  145.  
  146. /* Allocate and initialize a shading. */
  147. /* Free variables: mem, params, ppsh, psh. */
  148. #define ALLOC_SHADING(sttype, stype, sprocs, cname)\
  149.   BEGIN\
  150.     psh = gs_alloc_struct(mem, void, sttype, cname);\
  151.     if ( psh == 0 )\
  152.       return_error(gs_error_VMerror);\
  153.     psh->head.type = stype;\
  154.     psh->head.procs = sprocs;\
  155.     psh->params = *params;\
  156.     *ppsh = (gs_shading_t *)psh;\
  157.   END
  158.  
  159. /* ---------------- Function-based shading ---------------- */
  160.  
  161. private_st_shading_Fb();
  162.  
  163. /* Initialize parameters for a Function-based shading. */
  164. void
  165. gs_shading_Fb_params_init(gs_shading_Fb_params_t * params)
  166. {
  167.     shading_params_init((gs_shading_params_t *)params);
  168.     params->Domain[0] = params->Domain[2] = 0;
  169.     params->Domain[1] = params->Domain[3] = 1;
  170.     gs_make_identity(¶ms->Matrix);
  171.     params->Function = 0;    /* must be set by client */
  172. }
  173.  
  174. /* Allocate and initialize a Function-based shading. */
  175. private const gs_shading_procs_t shading_Fb_procs = {
  176.     gs_shading_Fb_fill_rectangle
  177. };
  178. int
  179. gs_shading_Fb_init(gs_shading_t ** ppsh,
  180.            const gs_shading_Fb_params_t * params, gs_memory_t * mem)
  181. {
  182.     gs_shading_Fb_t *psh;
  183.     gs_matrix imat;
  184.     int code = check_CBFD((const gs_shading_params_t *)params,
  185.               params->Function, params->Domain, 2);
  186.  
  187.     if (code < 0 ||
  188.     (code = gs_matrix_invert(¶ms->Matrix, &imat)) < 0
  189.     )
  190.     return code;
  191.     ALLOC_SHADING(&st_shading_Fb, shading_type_Function_based,
  192.           shading_Fb_procs, "gs_shading_Fb_init");
  193.     return 0;
  194. }
  195.  
  196. /* ---------------- Axial shading ---------------- */
  197.  
  198. private_st_shading_A();
  199.  
  200. /* Initialize parameters for an Axial shading. */
  201. void
  202. gs_shading_A_params_init(gs_shading_A_params_t * params)
  203. {
  204.     shading_params_init((gs_shading_params_t *)params);
  205.     /* Coords must be set by client */
  206.     params->Domain[0] = 0;
  207.     params->Domain[1] = 1;
  208.     params->Function = 0;    /* must be set by client */
  209.     params->Extend[0] = params->Extend[1] = false;
  210. }
  211.  
  212. /* Allocate and initialize an Axial shading. */
  213. private const gs_shading_procs_t shading_A_procs = {
  214.     gs_shading_A_fill_rectangle
  215. };
  216. int
  217. gs_shading_A_init(gs_shading_t ** ppsh,
  218.           const gs_shading_A_params_t * params, gs_memory_t * mem)
  219. {
  220.     gs_shading_A_t *psh;
  221.     int code = check_CBFD((const gs_shading_params_t *)params,
  222.               params->Function, params->Domain, 1);
  223.  
  224.     if (code < 0)
  225.     return code;
  226.     ALLOC_SHADING(&st_shading_A, shading_type_Axial,
  227.           shading_A_procs, "gs_shading_A_init");
  228.     return 0;
  229. }
  230.  
  231. /* ---------------- Radial shading ---------------- */
  232.  
  233. private_st_shading_R();
  234.  
  235. /* Initialize parameters for a Radial shading. */
  236. void
  237. gs_shading_R_params_init(gs_shading_R_params_t * params)
  238. {
  239.     shading_params_init((gs_shading_params_t *)params);
  240.     /* Coords must be set by client */
  241.     params->Domain[0] = 0;
  242.     params->Domain[1] = 1;
  243.     params->Function = 0;    /* must be set by client */
  244.     params->Extend[0] = params->Extend[1] = false;
  245. }
  246.  
  247. /* Allocate and initialize a Radial shading. */
  248. private const gs_shading_procs_t shading_R_procs = {
  249.     gs_shading_R_fill_rectangle
  250. };
  251. int
  252. gs_shading_R_init(gs_shading_t ** ppsh,
  253.           const gs_shading_R_params_t * params, gs_memory_t * mem)
  254. {
  255.     gs_shading_R_t *psh;
  256.     int code = check_CBFD((const gs_shading_params_t *)params,
  257.               params->Function, params->Domain, 1);
  258.  
  259.     if (code < 0)
  260.     return code;
  261.     if ((params->Domain != 0 && params->Domain[0] == params->Domain[1]) ||
  262.     params->Coords[2] < 0 || params->Coords[5] < 0
  263.     )
  264.     return_error(gs_error_rangecheck);
  265.     ALLOC_SHADING(&st_shading_R, shading_type_Radial,
  266.           shading_R_procs, "gs_shading_R_init");
  267.     return 0;
  268. }
  269.  
  270. /* ---------------- Free-form Gouraud triangle mesh shading ---------------- */
  271.  
  272. private_st_shading_FfGt();
  273.  
  274. /* Initialize parameters for a Free-form Gouraud triangle mesh shading. */
  275. void
  276. gs_shading_FfGt_params_init(gs_shading_FfGt_params_t * params)
  277. {
  278.     mesh_shading_params_init((gs_shading_mesh_params_t *)params);
  279.     /* Client must set BitsPerFlag if DataSource is not an array. */
  280. }
  281.  
  282. /* Allocate and initialize a Free-form Gouraud triangle mesh shading. */
  283. private const gs_shading_procs_t shading_FfGt_procs = {
  284.     gs_shading_FfGt_fill_rectangle
  285. };
  286. int
  287. gs_shading_FfGt_init(gs_shading_t ** ppsh,
  288.              const gs_shading_FfGt_params_t * params,
  289.              gs_memory_t * mem)
  290. {
  291.     gs_shading_FfGt_t *psh;
  292.     int code = check_mesh((const gs_shading_mesh_params_t *)params);
  293.     int bpf = check_BPF(¶ms->DataSource, params->BitsPerFlag);
  294.  
  295.     if (code < 0)
  296.     return code;
  297.     if (bpf < 0)
  298.     return bpf;
  299.     if (params->Decode != 0 && params->Decode[0] == params->Decode[1])
  300.     return_error(gs_error_rangecheck);
  301.     ALLOC_SHADING(&st_shading_FfGt, shading_type_Free_form_Gouraud_triangle,
  302.           shading_FfGt_procs, "gs_shading_FfGt_init");
  303.     psh->params.BitsPerFlag = bpf;
  304.     return 0;
  305. }
  306.  
  307. /* -------------- Lattice-form Gouraud triangle mesh shading -------------- */
  308.  
  309. private_st_shading_LfGt();
  310.  
  311. /* Initialize parameters for a Lattice-form Gouraud triangle mesh shading. */
  312. void
  313. gs_shading_LfGt_params_init(gs_shading_LfGt_params_t * params)
  314. {
  315.     mesh_shading_params_init((gs_shading_mesh_params_t *)params);
  316.     /* Client must set VerticesPerRow. */
  317. }
  318.  
  319. /* Allocate and initialize a Lattice-form Gouraud triangle mesh shading. */
  320. private const gs_shading_procs_t shading_LfGt_procs = {
  321.     gs_shading_LfGt_fill_rectangle
  322. };
  323. int
  324. gs_shading_LfGt_init(gs_shading_t ** ppsh,
  325.          const gs_shading_LfGt_params_t * params, gs_memory_t * mem)
  326. {
  327.     gs_shading_LfGt_t *psh;
  328.     int code = check_mesh((const gs_shading_mesh_params_t *)params);
  329.  
  330.     if (code < 0)
  331.     return code;
  332.     if (params->VerticesPerRow < 2)
  333.     return_error(gs_error_rangecheck);
  334.     ALLOC_SHADING(&st_shading_LfGt, shading_type_Lattice_form_Gouraud_triangle,
  335.           shading_LfGt_procs, "gs_shading_LfGt_init");
  336.     return 0;
  337. }
  338.  
  339. /* ---------------- Coons patch mesh shading ---------------- */
  340.  
  341. private_st_shading_Cp();
  342.  
  343. /* Initialize parameters for a Coons patch mesh shading. */
  344. void
  345. gs_shading_Cp_params_init(gs_shading_Cp_params_t * params)
  346. {
  347.     mesh_shading_params_init((gs_shading_mesh_params_t *)params);
  348.     /* Client must set BitsPerFlag if DataSource is not an array. */
  349. }
  350.  
  351. /* Allocate and initialize a Coons patch mesh shading. */
  352. private const gs_shading_procs_t shading_Cp_procs = {
  353.     gs_shading_Cp_fill_rectangle
  354. };
  355. int
  356. gs_shading_Cp_init(gs_shading_t ** ppsh,
  357.            const gs_shading_Cp_params_t * params, gs_memory_t * mem)
  358. {
  359.     gs_shading_Cp_t *psh;
  360.     int code = check_mesh((const gs_shading_mesh_params_t *)params);
  361.     int bpf = check_BPF(¶ms->DataSource, params->BitsPerFlag);
  362.  
  363.     if (code < 0)
  364.     return code;
  365.     if (bpf < 0)
  366.     return bpf;
  367.     ALLOC_SHADING(&st_shading_Cp, shading_type_Coons_patch,
  368.           shading_Cp_procs, "gs_shading_Cp_init");
  369.     psh->params.BitsPerFlag = bpf;
  370.     return 0;
  371. }
  372.  
  373. /* ---------------- Tensor product patch mesh shading ---------------- */
  374.  
  375. private_st_shading_Tpp();
  376.  
  377. /* Initialize parameters for a Tensor product patch mesh shading. */
  378. void
  379. gs_shading_Tpp_params_init(gs_shading_Tpp_params_t * params)
  380. {
  381.     mesh_shading_params_init((gs_shading_mesh_params_t *)params);
  382.     /* Client must set BitsPerFlag if DataSource is not an array. */
  383. }
  384.  
  385. /* Allocate and initialize a Tensor product patch mesh shading. */
  386. private const gs_shading_procs_t shading_Tpp_procs = {
  387.     gs_shading_Tpp_fill_rectangle
  388. };
  389. int
  390. gs_shading_Tpp_init(gs_shading_t ** ppsh,
  391.           const gs_shading_Tpp_params_t * params, gs_memory_t * mem)
  392. {
  393.     gs_shading_Tpp_t *psh;
  394.     int code = check_mesh((const gs_shading_mesh_params_t *)params);
  395.     int bpf = check_BPF(¶ms->DataSource, params->BitsPerFlag);
  396.  
  397.     if (code < 0)
  398.     return code;
  399.     if (bpf < 0)
  400.     return bpf;
  401.     ALLOC_SHADING(&st_shading_Tpp, shading_type_Tensor_product_patch,
  402.           shading_Tpp_procs, "gs_shading_Tpp_init");
  403.     psh->params.BitsPerFlag = bpf;
  404.     return 0;
  405. }
  406.  
  407. /* ================ Shading rendering ================ */
  408.  
  409. /* Add a user-space rectangle to a path. */
  410. private int
  411. shading_path_add_box(gx_path *ppath, const gs_rect *pbox,
  412.              const gs_matrix_fixed *pmat)
  413. {
  414.     gs_fixed_point pt;
  415.     gs_fixed_point pts[3];
  416.     int code;
  417.  
  418.     if ((code = gs_point_transform2fixed(pmat, pbox->p.x, pbox->p.y,
  419.                      &pt)) < 0 ||
  420.     (code = gx_path_add_point(ppath, pt.x, pt.y)) < 0 ||
  421.     (code = gs_point_transform2fixed(pmat, pbox->q.x, pbox->p.y,
  422.                      &pts[0])) < 0 ||
  423.     (code = gs_point_transform2fixed(pmat, pbox->q.x, pbox->q.y,
  424.                      &pts[1])) < 0 ||
  425.     (code = gs_point_transform2fixed(pmat, pbox->p.x, pbox->q.y,
  426.                      &pts[2])) < 0 ||
  427.     (code = gx_path_add_lines(ppath, pts, 3)) < 0
  428.     )
  429.     DO_NOTHING;
  430.     return code;
  431. }
  432.  
  433. /* Fill a path with a shading. */
  434. int
  435. gs_shading_fill_path(const gs_shading_t *psh, /*const*/ gx_path *ppath,
  436.              const gs_fixed_rect *prect, gx_device *orig_dev,
  437.              gs_imager_state *pis, bool fill_background)
  438. {
  439.     gs_memory_t *mem = pis->memory;
  440.     const gs_matrix_fixed *pmat = &pis->ctm;
  441.     gx_device *dev = orig_dev;
  442.     gs_fixed_rect path_box;
  443.     gs_rect rect;
  444.     gx_clip_path *path_clip = 0;
  445.     bool path_clip_set = false;
  446.     gx_device_clip path_dev;
  447.     int code = 0;
  448.  
  449.     path_clip = gx_cpath_alloc(mem, "shading_fill_path(path_clip)");
  450.     if (path_clip == 0) {
  451.     code = gs_note_error(gs_error_VMerror);
  452.     goto out;
  453.     }
  454.     dev_proc(dev, get_clipping_box)(dev, &path_box);
  455.     if (prect)
  456.     rect_intersect(path_box, *prect);
  457.     if (psh->params.have_BBox) {
  458.     gs_fixed_rect bbox_fixed;
  459.  
  460.     if ((is_xxyy(pmat) || is_xyyx(pmat)) &&
  461.         (code = shade_bbox_transform2fixed(&psh->params.BBox, pis,
  462.                            &bbox_fixed)) >= 0
  463.         ) {
  464.         /* We can fold BBox into the clipping rectangle. */
  465.         rect_intersect(path_box, bbox_fixed);
  466.     } else
  467.         {
  468.         gx_path *box_path;
  469.  
  470.         if (path_box.p.x >= path_box.q.x || path_box.p.y >= path_box.q.y)
  471.         goto out;        /* empty rectangle */
  472.         box_path = gx_path_alloc(mem, "shading_fill_path(box_path)");
  473.         if (box_path == 0) {
  474.         code = gs_note_error(gs_error_VMerror);
  475.         goto out;
  476.         }
  477.         if ((code = gx_cpath_from_rectangle(path_clip, &path_box)) < 0 ||
  478.         (code = shading_path_add_box(box_path, &psh->params.BBox,
  479.                          pmat)) < 0 ||
  480.         (code = gx_cpath_intersect(path_clip, box_path,
  481.                        gx_rule_winding_number, pis)) < 0
  482.         )
  483.         DO_NOTHING;
  484.         gx_path_free(box_path, "shading_fill_path(box_path)");
  485.         if (code < 0)
  486.         goto out;
  487.         path_clip_set = true;
  488.     }
  489.     }
  490.     if (!path_clip_set) {
  491.     if (path_box.p.x >= path_box.q.x || path_box.p.y >= path_box.q.y)
  492.         goto out;        /* empty rectangle */
  493.     if ((code = gx_cpath_from_rectangle(path_clip, &path_box)) < 0)
  494.         goto out;
  495.     }
  496.     if (ppath &&
  497.     (code =
  498.      gx_cpath_intersect(path_clip, ppath, gx_rule_winding_number, pis)) < 0
  499.     )
  500.     goto out;
  501.     gx_make_clip_device(&path_dev, &path_clip->rect_list->list);
  502.     path_dev.target = dev;
  503.     dev = (gx_device *)&path_dev;
  504.     dev_proc(dev, open_device)(dev);
  505.     dev_proc(dev, get_clipping_box)(dev, &path_box);
  506.     if (psh->params.Background && fill_background) {
  507.     int x0 = fixed2int(path_box.p.x);
  508.     int y0 = fixed2int(path_box.p.y);
  509.     int x1 = fixed2int(path_box.q.x);
  510.     int y1 = fixed2int(path_box.q.y);
  511.     const gs_color_space *pcs = psh->params.ColorSpace;
  512.     gs_client_color cc;
  513.     gx_device_color dev_color;
  514.  
  515.     cc = *psh->params.Background;
  516.     (*pcs->type->restrict_color)(&cc, pcs);
  517.     (*pcs->type->remap_color)(&cc, pcs, &dev_color, pis,
  518.                   dev, gs_color_select_texture);
  519.     /****** WRONG IF NON-IDEMPOTENT RasterOp ******/
  520.     code = gx_fill_rectangle_device_rop(x0, y0, x1 - x0, y1 - y0,
  521.                         &dev_color, dev, pis->log_op);
  522.     if (code < 0)
  523.         goto out;
  524.     }
  525.     {
  526.     gs_rect path_rect;
  527.  
  528.     path_rect.p.x = fixed2float(path_box.p.x);
  529.     path_rect.p.y = fixed2float(path_box.p.y);
  530.     path_rect.q.x = fixed2float(path_box.q.x);
  531.     path_rect.q.y = fixed2float(path_box.q.y);
  532.     gs_bbox_transform_inverse(&path_rect, (const gs_matrix *)pmat, &rect);
  533.     }
  534.     code = gs_shading_fill_rectangle(psh, &rect, dev, pis);
  535. out:
  536.     if (path_clip)
  537.     gx_cpath_free(path_clip, "shading_fill_path(path_clip)");
  538.     return code;
  539. }
  540.